package com.kobeliu.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kobeliu.base.BaseInfoProperties;
import com.kobeliu.enums.JobStatus;
import com.kobeliu.mapper.ResumeExpectMapper;
import com.kobeliu.pojo.ResumeExpect;
import com.kobeliu.pojo.bo.EditResumeExpectBO;
import com.kobeliu.pojo.vo.ResumeExpectVO;
import com.kobeliu.service.ResumeExpectService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ResumeExpectServiceImpl extends BaseInfoProperties implements ResumeExpectService {

    @Autowired
    private ResumeExpectMapper resumeExpectMapper;

    @Transactional
    @Override
    public void save(EditResumeExpectBO bo) {

        ResumeExpect expect = new ResumeExpect();
        BeanUtils.copyProperties(bo,expect);
        expect.setUpdatedTime(LocalDateTime.now());

        if(StringUtils.isNotBlank(bo.getId())){
            //如果id不为0 则为update
            resumeExpectMapper.updateById(expect);
        }else{
            expect.setCreateTime(LocalDateTime.now());
            resumeExpectMapper.insert(expect);
        }

        // 删除对应的缓存
        redis.del(REDIS_RESUME_EXPECT+":"+expect.getUserId());

    }

    @Override
    public List<ResumeExpectVO> getList(String resumeId, String userId, JobStatus status) {

        List<ResumeExpectVO> vos = new ArrayList<>();

        if(resumeId == null || resumeId == "" || resumeId=="undefined"){
            return vos;
        }
        QueryWrapper<ResumeExpect> qw = new QueryWrapper<ResumeExpect>()
                .eq("resume_id", resumeId)
                .eq("user_id", userId);
        if(status != null){
            qw.eq("status",status.type);
        }
        List<ResumeExpect> resumeExpects = resumeExpectMapper.selectList(qw);

        vos = resumeExpects.stream().map(obj -> {
            ResumeExpectVO vo = new ResumeExpectVO();
            BeanUtils.copyProperties(obj, vo);
            return vo;
        }).collect(Collectors.toList());

        return vos;
    }

    @Transactional
    @Override
    public void delete(String resumeExpectId, String userId) {

        resumeExpectMapper.delete(new QueryWrapper<ResumeExpect>()
                .eq("id",resumeExpectId)
                .eq("user_id",userId));

        // 删除对应的缓存
        redis.del(REDIS_RESUME_EXPECT+":"+userId);
    }
}
